package com.bestom.hardwaretest.activity;

import android.hardware.Camera;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import com.bestom.hardwaretest.R;
import com.bestom.hardwaretest.util.ToastUtil;
import java.io.IOException;

public class CameraActivity extends BaseActivity {
    private Camera mCamera;
    private SurfaceHolder mHolder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_camera);
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item){
        if(item.getItemId()==android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView(){
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("Camera");
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        findViewById(R.id.camera0).setOnClickListener(view -> openCamera(0));
        findViewById(R.id.camera1).setOnClickListener(view -> openCamera(1));
        findViewById(R.id.camera2).setOnClickListener(view -> openCamera(2));
        SurfaceView surfaceView = findViewById(R.id.surfaceView);
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        //设置Surface分辨率
        surfaceView.getHolder().setFixedSize(176, 144);
        // 屏幕常亮
        surfaceView.getHolder().setKeepScreenOn(true);
        //为SurfaceView的句柄添加一个回调函数
        surfaceView.getHolder().addCallback(new SurfaceCallback());
    }

    private void openCamera(int i){
        if (Camera.getNumberOfCameras() > i){
            if (mCamera != null){
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }

            mCamera = Camera.open(i);
            try {
                //通过surfaceview显示取景画面
                mCamera.setPreviewDisplay(mHolder);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //开始预览
            mCamera.startPreview();
        }else {
            ToastUtil.toast("无法打开摄像头" + i);
        }
    }

    /**
     * 显示surfaceView 数据的接口
     */
    private class SurfaceCallback implements SurfaceHolder.Callback {

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                   int height) {
            Camera.Parameters mParameters = mCamera.getParameters(); // 获取各项参数
            mParameters.setPreviewSize(width, height); // 设置预览大小
            mParameters.setPreviewFrameRate(5);  //设置每秒显示4帧
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // SurfaceHolder是系统提供用来设置surfaceView的对象
            mHolder = holder;
            try {
                mCamera = Camera.open();
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if (mCamera != null) {
                mCamera.release(); // 释放照相机
                mCamera = null;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCamera != null) {
            mCamera.release(); // 释放照相机
            mCamera = null;
        }
    }
}
